/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.model;

import java.util.List;
import edu.syr.iis.web.util.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * Manages database operations for ConversationLabel table
 * @author kinoue
 *
 */
public class ConversationLabelManager
{
    /**
     * Returns list of all ConversationLabel database records sorted by daasetId
     */
    public List<ConversationLabel> getConversationLabels()
    {
	List<ConversationLabel> list = null;
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();
	
	try
	{
	    list = session.createQuery("FROM ConversationLabel ORDER BY conversationLabelId").list();
	    session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
	    session.getTransaction().rollback();
	    throw e;
	}
	return list;
    }
    
    public ConversationLabel getConversationLabel(int conversationLabelId)
    {
	ConversationLabel conversationLabel = null;
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();
	
	try
	{
	    conversationLabel = (ConversationLabel) session.createQuery(
		    "FROM ConversationLabel " +
		    " WHERE conversationLabelId = ?").setInteger(0, conversationLabelId).uniqueResult();
	    session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
	    session.getTransaction().rollback();
	    throw e;
	}
	return conversationLabel;
    }
    
    /**
     * Saves a ConversationLabel object.
     */
    public void saveConversationLabel(ConversationLabel conversationLabel)
    {
        Session session = HibernateUtil.getSessionFactory()
                .getCurrentSession();
        session.beginTransaction();
        try
        {
            session.saveOrUpdate(conversationLabel);
            session.getTransaction().commit();
        }
        catch (HibernateException e)
        {
            session.getTransaction().rollback();
            throw e;
        }
    }
    
    /**
     * Deletes ConversationLabel record with matching conversationLabelId 
     */
    public void deleteConversationLabel(int conversationLabelId)
    {
        Session session = 
            	HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();	
        try
        {
            session.delete(session.load(ConversationLabel.class,new Integer(conversationLabelId)));         	    			
            session.flush();
            session.getTransaction().commit();
        }
        catch (HibernateException e)
        {
            session.getTransaction().rollback();
            throw e;
        }
    }
}
